import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/unit/page/addUnit/add_unit_page.dart';
import 'package:boss/ui/page/unit/unit_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/loader_error.dart';
import 'package:boss/ui/widget/loader/loader_loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(UnitManagerPage.pageName)
class UnitManagerPage extends StatefulWidget {
  static const pageName = '/UnitManagerPage';

  @override
  _UnitManagerPageState createState() => _UnitManagerPageState();
}

class _UnitManagerPageState extends BaseState<UnitManagerPage> {
  _UnitManagerPageState() : super(UnitManagerPage.pageName);

  int status = 0;

  List<String> units = [];

  @override
  void initState() {
    super.initState();

    initData();
  }

  initData() async {
    setState(() {
      status = 0;
    });

    final res = await Provider.of<UnitProvider>(context, listen: false).queryUnit();
    if (res.isError) {
      status = -1;
    } else {
      status = 1;
      units = res.asValue.value;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '管理单位',
        actions: [
          LabelButton(
            title: '完成',
            padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
            textStyle: TextStyleConstant().blue_28,
            onTap: () async {
              final flag = await Provider.of<UnitProvider>(context, listen: false).updateUnit(units);

              if (flag) {
                Navigator.pop(context);
              }
            },
          )
        ],
      ),
      body: Builder(
        builder: (context) {
          if (status == 0) {
            return LoaderLoading();
          }

          if (status == -1) {
            return LoaderError(
              errorText: '数据加载失败',
              onTap: initData,
            );
          }

          return ListView.builder(
            padding: EdgeInsets.symmetric(horizontal: 30.w),
            itemBuilder: (context, index) {
              if (index == units.length) {
                return CustomListTitle(
                  title: '',
                  isHideDivider: true,
                  trailing: LabelButton(
                    title: '添加单位',
                    textStyle: TextStyleConstant().blue_28,
                    onTap: () async {
                      final res = await Navigator.pushNamed(context, AddUnitPage.pageName, arguments: {'units': units});
                      if (res != null) {
                        units.add(res);
                        setState(() {});
                      }
                    },
                  ),
                );
              }
              return CustomListTitle(
                title: units[index],
                trailing: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    showAlert(
                      context,
                      content: '确定要删除该单位吗?',
                      onConfirm: (context) {
                        Navigator.pop(context);
                        units.removeAt(index);
                        setState(() {});
                      },
                      onCancel: (context) {
                        Navigator.pop(context);
                      },
                    );
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(4.0),
                    child: BaseImage(
                      'assets/image/delete_icon.png',
                      width: 30.w,
                      height: 30.w,
                    ),
                  ),
                ),
              );
            },
            itemCount: units.length + 1,
          );
        },
      ),
    );
  }
}
